package TanChiShe;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.*;

public class HuaBu extends JPanel implements KeyListener,ActionListener {
	
		/**
	 * 
	 */
	private static final long serialVersionUID = -3800264560009921376L;
		ImageIcon title = new ImageIcon("title.png");
		ImageIcon up = new ImageIcon("up.png");
		ImageIcon down = new ImageIcon("down.png");
		ImageIcon left = new ImageIcon("left.png");
		ImageIcon right = new ImageIcon("right.png");
		ImageIcon food = new ImageIcon("food.png");
		ImageIcon body = new ImageIcon("body.png");
		
		//蛇的数据结构的设计
		int [] snakex = new int[750];
		int [] snakey = new int[850];
		int len = 3;
		String fanx = "R";//R右L左U上D下
		
		//食物设计
		Random r = new Random();
		int foodx = r.nextInt(34)*25+25;
		int foody = r.nextInt(24)*25+75;
		
		//游戏是否开始
		boolean  goend = false;
		
		//游戏是否失败
		boolean isend = false;
		
		Timer timer = new Timer(150,this);	
		
		//统计分数
		int fen = 0;
		
		public HuaBu() {
			this.setFocusable(true);
			initSnaKe();
			this.addKeyListener(this);
			timer.start();
		}
		
		//初始化蛇
		public void initSnaKe() {
			goend = false;
			isend = false;
			len = 3;
			fanx = "R";
			snakex[0] = 100;
			snakey[0] = 100;
			snakex[1] = 75;
			snakey[1] = 100;
			snakex[2] = 50;
			snakey[2] = 100;
		}
		
	public void paint(Graphics g) {
		
		this.setBackground(Color.black);
		g.fillRect(25,75,850,600);
		title.paintIcon(this, g, 25,11);
		
		//画蛇头
		if(fanx.equals("R")) {
			right.paintIcon(this, g,snakex[0],snakey[0]);
		}else if(fanx.equals("L")) {
			left.paintIcon(this, g,snakex[0],snakey[0]);
		}else if(fanx.equals("U")) {
			up.paintIcon(this, g,snakex[0],snakey[0]);
		}else if(fanx.equals("D")) {
			down.paintIcon(this, g,snakex[0],snakey[0]);
		}
		//画蛇身
		for(int i=1;i<len;i++) {
			body.paintIcon(this, g,snakex[i], snakey[i]);
		}
		if(!goend) {
			g.setColor(Color.WHITE);
			g.setFont(new Font("楷体",Font.CENTER_BASELINE,30));
			g.drawString("等鱼不懂英语所以用中文Start/Pause",200,350);
		}
		
		//失败提示
		if(isend){
			g.setColor(Color.WHITE);
			g.setFont(new Font("楷体",Font.CENTER_BASELINE,30));
			g.drawString("你个憨批这就玩完了，哈哈哈哈嗝~",200,350);
			fen = 0;
		}
		//画食物
		food.paintIcon(this,g,foodx,foody);
		//分数和长度统计
		g.setColor(Color.WHITE);
		g.setFont(new Font("黑体",Font.CENTER_BASELINE,15));
		g.drawString("分数："+fen,750,30);
		g.setColor(Color.WHITE);
		g.setFont(new Font("黑体",Font.CENTER_BASELINE,15));
		g.drawString("长度："+len,750,50);
	}

	//开始提示语
		
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}
	


	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if(keyCode == KeyEvent.VK_SPACE) {
			if(isend) {
				initSnaKe();
			}else {
				goend = !goend;
			}
			repaint();
		}else if(keyCode == KeyEvent.VK_UP && !fanx.equals("D")) {
			fanx = "U";
		}else if(keyCode == KeyEvent.VK_DOWN && !fanx.equals("U")) {
			fanx = "D";
		}else if(keyCode == KeyEvent.VK_LEFT && !fanx.equals("R")) {
			fanx = "L";
		}else if(keyCode == KeyEvent.VK_RIGHT && !fanx.equals("L")) {
			fanx = "R";
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO 自动生成的方法存根
		
	}

	/*
	 * 1.重新定个闹钟
	 * 2.蛇移动
	 * 3.重新绘制界面
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		timer.start();
		if(goend && !isend) {
			//移动身体
			for(int i=len;i>0;i--) {
				snakex[i] = snakex[i-1];
				snakey[i] = snakey[i-1];
			}
			if(fanx.equals("R")) {
				//横坐标+25
				snakex[0] = snakex[0]+25;
				if(snakex[0] > 850) {
					snakex[0] = 25;
				}
			}else if(fanx.equals("L")) {
				snakex[0] = snakex[0]-25;
				if(snakex[0] < 25) {
					snakex[0] = 850;
				}
			}else if(fanx.equals("U")) {
				snakey[0] = snakey[0]-25;
				if(snakey[0] < 75) {
					snakey[0] = 650;
				}
			}else if(fanx.equals("D")) {
				snakey[0] = snakey[0]+25;
				if(snakey[0] > 650) {
					snakey[0] = 75;
				}
			}
			
			//吃食物
			if(snakex[0] == foodx && snakey[0] == foody) {
				len++;
				fen += 10;
				foodx = r.nextInt(34)*25+25;
				foody = r.nextInt(24)*25+75;
			}
			for(int i=1;i<len;i++) {
				if(snakex[0] == snakex[i] && snakey[0] == snakey[i]) {
					isend = true;
				}
			}
		}
		repaint();
	}

}
